package ui.activity.mine.presenter;


import com.google.gson.Gson;

import org.greenrobot.eventbus.EventBus;

import javax.inject.Inject;

import Bean.ChangePasswordBean;
import Bean.ChangePasswordParamBean;
import base.BaseBiz;
import coom.MyType;
import coom.RetrofitHelp;
import event.CloseActEvent;
import okhttp3.MediaType;
import okhttp3.RequestBody;
import ui.activity.login.LoginAct;
import ui.activity.mine.biz.ChangePwdBiz;
import ui.activity.mine.contract.ChangePwdContract;
import util.StringUtils;

/**
 * Created by chenjian on 19/2/26
 */

public class ChangePwdPresenter implements ChangePwdContract.Presenter {

    ChangePwdContract.View view;
    ChangePwdBiz changePwdBiz;


    @Inject
    public ChangePwdPresenter(ChangePwdContract.View view) {
        this.view = view;
    }


    @Override
    public void setBiz(BaseBiz biz) {
        changePwdBiz = (ChangePwdBiz) biz;
    }


    @Override
    public void setPwd(String id, String oldPwd, String newPwd, String surePwd) {
        if (StringUtils.isEmpty(oldPwd) || StringUtils.isEmpty(newPwd) || StringUtils.isEmpty(surePwd)) {
            view.showMsg("请完善信息");
            return;
        }
        if (!newPwd.equals(surePwd)) {
            view.showMsg("新密码与确认密码不一致");
            return;
        }
        view.loading(true);
        ChangePasswordParamBean c = new ChangePasswordParamBean();
        c.setCustomId(id);
        c.setOldpassword(oldPwd);
        c.setNewpassword(newPwd);
        c.setRePassWord(surePwd);
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), new Gson().toJson(c));
        changePwdBiz.getPwd(requestBody, new BaseBiz.Callback<ChangePasswordBean.DataBean>() {
            @Override
            public void onSuccess(ChangePasswordBean.DataBean info) {
                view.loading(false);
                view.showMsg("修改密码成功");
                RetrofitHelp.logout();
                view.startAct(LoginAct.class, null);
                EventBus.getDefault().post(new CloseActEvent(MyType.Logout, ""));
                view.finishAct();

            }

            @Override
            public void onFailure(ChangePasswordBean.DataBean info) {
                view.loading(false);
            }
        });
    }
}
